/*
 * @Author: chestnut_647 
 * @Date: 2017-07-21 13:44:29 
 * @Last Modified by: chestnut_647
 * @Last Modified time: 2017-08-02 18:45:44
 */
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide  = require('page/common/nav-side/index.js');
var Pagination = require('util/pagination/index.js');
var _mm = require('util/mm.js');
var _order = require('service/order-service.js');
var templateIndex = require('./index.string');

var page = {
  data: {
    listParam: {
      pageSize: 10,
      pageNum: 1
    }
  },
  init: function() {
    this.onLoad();
  },
  onLoad: function() {
    // 初始化左侧菜单
    navSide.init({name: 'order-list'});
    // 加载用户信息
    this.loadOrderList();
  },
  loadOrderList: function() {
    var _this = this;
    _order.getOrderList(_this.data.listParam, function(res) {

      // 渲染html
      var listHtml = _mm.renderHtml(templateIndex, res);
      $('.order-list-con').html(listHtml);
      // 加载分页信息
      _this.loadPagination({
        hasPreviousPage: res.hasPreviousPage,
        prePage        : res.prePage,
        hasNextPage    : res.hasNextPage,
        nextPage       : res.nextPage,
        pageNum        : res.pageNum,
        pages          : res.pages,
      });
    }, function(errMsg) {
      $('.order-list-con').html('<p class="err-tip">加载订单失败，请刷新后重试</p>');

    })
  },

  loadPagination: function(pageInfo) {
    var _this = this;
    this.pagination ? '' : (this.pagination = new Pagination());
    this.pagination.render($.extend({}, pageInfo, {
      // 父组件传递子组件，通过传参的方式
      container: $('.pagination'),
      // 子组件传递数据给父组件，得到value值
      onSelectPage: function(pageNum) {
        _this.data.listParam.pageNum = pageNum;
        _this.loadOrderList();
      }
    }));
  }
  
};
$(function() {
  page.init();
  
})